package com.app.base.service.core.pay.service.impl;

import com.app.base.service.auth.entity.SysUser;
import com.app.base.service.auth.service.SysUserService;
import com.app.base.service.core.organization.entity.Organization;
import com.app.base.service.core.organization.service.OrganizationService;
import com.app.base.service.core.pay.entity.OrgPay;
import com.app.base.service.core.pay.mapper.OrgPayMapper;
import com.app.base.service.core.pay.service.OrgPayService;
import com.app.base.utlis.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 岗位薪酬表 服务实现类
 * @author yuhag
 * @since 2022-06-12
 */
@Service
@Slf4j
public class OrgPayServiceImpl extends ServiceImpl<OrgPayMapper, OrgPay> implements OrgPayService {


    @Resource
    private OrganizationService organizationService;

    @Resource
    private SysUserService sysUserService;

    @Override
    public Result saveOrUpdateOrg(OrgPay orgPay) {
        try{

            String positionId = orgPay.getPositionId();
            Integer years = orgPay.getYears();
            String msg = "";
            OrgPay pay = baseMapper.selectOne(new LambdaQueryWrapper<OrgPay>()
                    .eq(OrgPay::getPositionId, positionId)
                    .eq(OrgPay::getYears, years));
            if (StringUtils.isEmpty(pay)){
                baseMapper.insert(orgPay);
                msg = "添加成功!";
            }else {
                orgPay.setId(pay.getId());
                baseMapper.updateById(orgPay);
                msg = "更新成功!";
            }

            return Result.ok().message(msg);
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result getOrgPay(Long current, Long pageSize) {
        try{
            Page<OrgPay> page = new Page<>(current,pageSize);
            QueryWrapper<OrgPay> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("position_id");
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);

            page.getRecords().forEach(item->{
                Organization organization = organizationService.getById(item.getPositionId());
                item.setPosition(organization.getOrganization());

                SysUser user = sysUserService.getById(item.getCreatedBy());
                item.setUsername(user.getRealName());

                String yearStr = (item.getYears()-1)+"~"+item.getYears()+"/年";
                item.setYearStr(yearStr);

            });

            return Result.ok(page);
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result delOrgPay(String id) {
        try{
            baseMapper.deleteById(id);
            return Result.ok();
        }catch(Exception e){
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}
